﻿using LCL.Network.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LCL.Tests.Common.Network.Profinet
{
    public class SiemensS7NetExample
    {

        public void ClassTest()
        {
            #region Usage

            // 实例化对象，指定PLC的ip地址
            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");
            // 举例读取M100的值
            short M100 = siemens.ReadInt16("M100").Data;

            #endregion
        }

        public void ClassTest2()
        {
            #region Usage2

            // 实例化对象，指定PLC的ip地址和端口号
            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 连接对象
            Result connect = siemens.ConnectServer();
            if (!connect.Success)
            {
                Console.WriteLine("connect failed:" + connect.Message);
                return;
            }

            // 举例读取M100的值
            short M100 = siemens.ReadInt16("M100").Data;

            siemens.ConnectClose();

            #endregion
        }


        public void ReadExample()
        {
            #region ReadExample1


            SiemensS7Net siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 此处以M100寄存器作为示例
            byte byte_M100 = siemensTcpNet.ReadByte("M100").Data; // 读取M100的值
            short short_M100 = siemensTcpNet.ReadInt16("M100").Data; // 读取M100-M101组成的字
            ushort ushort_M100 = siemensTcpNet.ReadUInt16("M100").Data; // 读取M100-M101组成的无符号的值
            int int_M100 = siemensTcpNet.ReadInt32("M100").Data;         // 读取M100-M103组成的有符号的数据
            uint uint_M100 = siemensTcpNet.ReadUInt32("M100").Data;      // 读取M100-M103组成的无符号的值
            float float_M100 = siemensTcpNet.ReadFloat("M100").Data;   // 读取M100-M103组成的单精度值
            long long_M100 = siemensTcpNet.ReadInt64("M100").Data;      // 读取M100-M107组成的大数据值
            ulong ulong_M100 = siemensTcpNet.ReadUInt64("M100").Data;   // 读取M100-M107组成的无符号大数据
            double double_M100 = siemensTcpNet.ReadDouble("M100").Data; // 读取M100-M107组成的双精度值
            string str_M100 = siemensTcpNet.ReadString("M100", 10).Data;// 读取M100-M109组成的ASCII字符串数据

            // 读取数组
            short[] short_M100_array = siemensTcpNet.ReadInt16("M100", 10).Data; // 读取M100-M101组成的字
            ushort[] ushort_M100_array = siemensTcpNet.ReadUInt16("M100", 10).Data; // 读取M100-M101组成的无符号的值
            int[] int_M100_array = siemensTcpNet.ReadInt32("M100", 10).Data;         // 读取M100-M103组成的有符号的数据
            uint[] uint_M100_array = siemensTcpNet.ReadUInt32("M100", 10).Data;      // 读取M100-M103组成的无符号的值
            float[] float_M100_array = siemensTcpNet.ReadFloat("M100", 10).Data;   // 读取M100-M103组成的单精度值
            long[] long_M100_array = siemensTcpNet.ReadInt64("M100", 10).Data;      // 读取M100-M107组成的大数据值
            ulong[] ulong_M100_array = siemensTcpNet.ReadUInt64("M100", 10).Data;   // 读取M100-M107组成的无符号大数据
            double[] double_M100_array = siemensTcpNet.ReadDouble("M100", 10).Data; // 读取M100-M107组成的双精度值

            #endregion
        }

        public void ReadExample2()
        {
            #region ReadExample2

            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            Result<byte[]> read = siemens.Read("M100", 8);
            if (read.Success)
            {
                float temp = siemens.ByteTransform.TransInt16(read.Data, 0) / 10f;
                float press = siemens.ByteTransform.TransInt16(read.Data, 2) / 100f;
                int count = siemens.ByteTransform.TransInt32(read.Data, 2);
                // do something
            }
            else
            {
                // failed
            }


            #endregion
        }

        public void WriteExample()
        {
            #region WriteExample1

            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 此处以M100寄存器作为示例
            siemens.Write("M100", (short)1234);                // 写入M100  short值  ,W3C0,R3C0 效果是一样的
            siemens.Write("M100", (ushort)45678);              // 写入M100  ushort值
            siemens.Write("M100", 1234566);                    // 写入M100  int值
            siemens.Write("M100", (uint)1234566);               // 写入M100  uint值
            siemens.Write("M100", 123.456f);                    // 写入M100  float值
            siemens.Write("M100", 123.456d);                    // 写入M100  double值
            siemens.Write("M100", 123456661235123534L);          // 写入M100  long值
            siemens.Write("M100", 523456661235123534UL);          // 写入M100  ulong值
            siemens.Write("M100", "K123456789");                // 写入M100  string值

            // 读取数组
            siemens.Write("M100", new short[] { 123, 3566, -123 });                // 写入M100  short值  ,W3C0,R3C0 效果是一样的
            siemens.Write("M100", new ushort[] { 12242, 42321, 12323 });              // 写入M100  ushort值
            siemens.Write("M100", new int[] { 1234312312, 12312312, -1237213 });                    // 写入M100  int值
            siemens.Write("M100", new uint[] { 523123212, 213, 13123 });               // 写入M100  uint值
            siemens.Write("M100", new float[] { 123.456f, 35.3f, -675.2f });                    // 写入M100  float值
            siemens.Write("M100", new double[] { 12343.542312d, 213123.123d, -231232.53432d });                    // 写入M100  double值
            siemens.Write("M100", new long[] { 1231231242312, 34312312323214, -1283862312631823 });          // 写入M100  long值
            siemens.Write("M100", new ulong[] { 1231231242312, 34312312323214, 9731283862312631823 });          // 写入M100  ulong值

            #endregion
        }

        public void WriteExample2()
        {
            #region WriteExample2

            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 拼凑数据，这样的话，一次通讯就完成数据的全部写入
            byte[] buffer = new byte[8];
            siemens.ByteTransform.TransByte((short)1234).CopyTo(buffer, 0);
            siemens.ByteTransform.TransByte((short)2100).CopyTo(buffer, 2);
            siemens.ByteTransform.TransByte(12353423).CopyTo(buffer, 4);

            Result write = siemens.Write("M100", buffer);
            if (write.Success)
            {
                // success
            }
            else
            {
                // failed
            }

            // 上面的功能等同于三个数据分别写入，下面的性能更差点，因为进行了三次通讯，而且每次还要判断是否写入成功
            //siemens.Write( "M100", (short)1234 );
            //siemens.Write( "M100", (short)2100 );
            //siemens.Write( "M100", 12353423 );

            #endregion
        }


        public void ReadBool()
        {
            #region ReadBool

            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 以下是简单的读取，没有仔细校验的方式
            bool X1 = siemens.ReadBool("M100.0").Data;

            // 如果需要判断是否读取成功
            Result<bool> R_X1 = siemens.ReadBool("M100.0");
            if (R_X1.Success)
            {
                // success
                bool value = R_X1.Data;
            }
            else
            {
                // failed
            }


            #endregion
        }

        public void WriteBool()
        {
            #region WriteBool

            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 以下是简单的写入，没有仔细校验的方式
            siemens.Write("M100.0", true);

            // 如果需要判断是否读取成功
            Result write1 = siemens.Write("M100.0", true);
            if (write1.Success)
            {
                // success
            }
            else
            {
                // failed
            }




            #endregion
        }

    }
}
